#include <STC15F2K60S2.H>
#include "Tim.h"
#include "Seg.h"
#include "Key.h"
#include "iic.h"
#include "stdio.h"
#include "LED.h"
//看情况尽量不用浮点数来读写，用char来实现比较简单，但是要注意类型的范围
//可以运用状态来实现跳转
//at24c02，adda的使用，灯，at24c02什么时候读写，ad什么时候读，结合数据刷新时间
//注意各个数据刷新时间
unsigned long ulms,ucStart;
unsigned char pucBuf[1];
unsigned char pucSeg_Buf[9],pucSeg_Code[8],ucSeg_Pos;
unsigned char ucKey_Dly,ucKey_Old;
unsigned int ucSeg_Dly;
unsigned char ucSec;
unsigned char ucState;
unsigned char ucpcf_U,ucPcf_Up;
unsigned char ucKey_num;
unsigned char ucLed;
unsigned char ucpcf_Uval;
unsigned char ucad_Flag,ucad_Old;
unsigned char ucad_Dly;
void Key_Proc(void);
void Seg_Proc(void);
void Init_Eerow(void);
void Led_Set(void);
void U_Set(unsigned char ucKey_Down);
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	Init_Eerow();
	while(1){
		Key_Proc();
		Seg_Proc();
		Led_Set();
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	 if(!(ulms % 100)) //在计时器中实现东西会影响数码管的显示，和这个同步的时候
	 { 
		 		
	} 
	 Led_Disp(ucLed) ;
	if(++ucad_Dly==100) ucad_Dly=0;
	 if(++ucSeg_Dly==300) ucSeg_Dly=0;
	 if(++ucKey_Dly==10) ucKey_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
}
void Init_Eerow(void){
	EEPROM_Read(pucBuf, 0, 1);
	if(pucBuf[0]>=0&&pucBuf[0]<=50){
		ucPcf_Up=pucBuf[0];
	}
	else
		ucPcf_Up=30;
}
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 switch(ucKey_Down)  { 
		 case 12:
			 if(++ucState==3) ucState=0;
			ucKey_num=0;
		 	if(ucState==2){
					pucBuf[0]=ucPcf_Up;
					EEPROM_Write(pucBuf,0, 1);
		}
			break;
		 case 13:
			 ucKey_num=0;
			 if(ucState==2)
			 ucSec=0;
			break;
		 case 16:
			 ucKey_num=0;
			 U_Set(ucKey_Down);
			break;
		 case 17://十六之后的键扫描不到，是因为p34和signal引脚跳线帽导致的
			 ucKey_num=0;
			U_Set(ucKey_Down);
			break;
		case 4:ucKey_num++;break;
		case 5:ucKey_num++;break;
		case 6:ucKey_num++;break;
		case 7:ucKey_num++;break;
		case 8:ucKey_num++;break;
		case 9:ucKey_num++;break;
		case 10:ucKey_num++;break;
		case 11:ucKey_num++;break;
		 case 14:ucKey_num++;break;
		 case 15:ucKey_num++;break;
		 case 18:ucKey_num++;break;
		 case 19:ucKey_num++;break;
	 } 
}
void Seg_Proc(void){
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	switch(ucState){
		case 0:
			sprintf(pucSeg_Buf,"U    %3.2f",ucpcf_U/51.0);
		break;
		case 1:
			sprintf(pucSeg_Buf,"P    %3.2f",ucPcf_Up/10.0);
			break;
		case 2:
			sprintf(pucSeg_Buf,"N %6d",(unsigned int)ucSec);
			break;
	}
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
void U_Set(unsigned char ucKey_Down){
	if(ucState==1){
		if(ucKey_Down==16){
			ucPcf_Up+=5;
			if(ucPcf_Up>50)
				ucPcf_Up=0;
		}
		else if(ucKey_Down==17){
			if(ucPcf_Up==0)
				ucPcf_Up=55;
				ucPcf_Up-=5;
		}
	}
}
void Led_Set(void){
	if((ulms-ucStart>=5000)&&(ucpcf_Uval<ucPcf_Up)){
		ucLed|=0x01;
	}
	else{
		if(ucpcf_Uval>=ucPcf_Up)
			ucStart=ulms;
		ucLed&=~1;
	}
		
	if(ucSec%2){
		ucLed|=0x02;
	}
	else{
		ucLed&=~2;
	}
	if(ucKey_num>=3){
		ucLed|=0x04;
	}
	else{
		ucLed&=~4;
	}
	if(ucad_Dly)  return;
	ucad_Dly=1;
	ucpcf_U=PCF8591_Adc();//读的时候需要时间，读一次大值在变成实际值，注意这个问题
	ucpcf_Uval=ucpcf_U/5.1;
	if(ucpcf_Uval>=ucPcf_Up){
		ucad_Flag=1;
	}
	else{
		ucad_Flag=0;
	}
	if(ucad_Flag!=ucad_Old){
		ucad_Old=ucad_Flag;
		if(ucad_Flag==0){
			ucSec++;
		}
	}
}